﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SplitCode;

namespace TiffSplit
{
    public partial class TiffSplit : Form
    {
        public TiffSplit()
        {
            InitializeComponent();

            _updateProgress = new UpdateProgress(UpdatePrgDel);
        }

        private ConfObj _configs = new ConfObj();
        private UpdateProgress _updateProgress;

        private void button_select_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "tif files (*.tif)|*.tif|tiff files (*.tiff)|*.tiff|PDF files (*.pdf)|*.pdf";
            DialogResult res = ofd.ShowDialog();
            if (res != DialogResult.OK)
                return;

            processFile(ofd.FileName);

        }

        private void button_select_DragDrop(object sender, DragEventArgs e)
        {
            Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
            if (a != null)
            {
                string fileName = a.GetValue(0).ToString();
                processFile(fileName);
            }

        }
       
        private void button_select_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void processFile(string fileName)
        {
            button_select.Enabled = false;
            button_select.AllowDrop = false;
            int numOfPages = 0;

            _configs.inputFile = fileName;
            RetObj retObj = TiffSplitCode.Prepare(_configs, out numOfPages);
            if (!retObj.isOK)
            {
                MessageBox.Show(retObj.msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (numOfPages == 0)
            {
                MessageBox.Show("Internal error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            PageSelection ps = new PageSelection(numOfPages);
            ps.ShowDialog();
            _configs.fromPage = ps.PageFrom;
            _configs.toPage = ps.PageTo;
            _configs.doOverwrite = ps.OverwriteFiles;
            
            progressBar.Value = 0;
            progressBar.Enabled = true;
            backgroundWorkerSplit.RunWorkerAsync(_configs);
        }

        private void backgroundWorkerSplit_DoWork(object sender, DoWorkEventArgs e)
        {
            ConfObj input = e.Argument as ConfObj;  
            RetObj retObj = TiffSplitCode.Split(input, _updateProgress);
            e.Result = retObj;
        }

        private void UpdatePrgDel(int progress)
        {
            backgroundWorkerSplit.ReportProgress(progress);
        }

        private void backgroundWorkerSplit_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar.Value = 0;
            progressBar.Enabled = false;
            button_select.Enabled = true;
            button_select.AllowDrop = true;

            RetObj retObj = e.Result as RetObj;
            if (retObj.isOK)
                MessageBox.Show("Done!");
            else
            {
                MessageBox.Show(retObj.msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void backgroundWorkerSplit_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar.Value = e.ProgressPercentage;
        }

        private void optionsButton_Click(object sender, EventArgs e)
        {
            Options optDiag = new Options(_configs);
            DialogResult res = optDiag.ShowDialog();
            if (res == DialogResult.Cancel)
                return;
        }


    }
}
